package com.alaia.product.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alaia.product.mapper.CategoryMapper;
import com.alaia.product.domain.Category;
import com.alaia.product.service.ICategoryService;

/**
 * 商品三级分类Service业务层处理
 *
 * @author Alaia
 * @date 2022-11-01
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品三级分类
     *
     * @param catId 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public Category selectCategoryByCatId(Long catId) {
        return categoryMapper.selectCategoryByCatId(catId);
    }

    /**
     * 查询商品三级分类列表
     *
     * @param category 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category) {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品三级分类
     *
     * @param catIds 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatIds(Long[] catIds) {
        return categoryMapper.deleteCategoryByCatIds(catIds);
    }

    /**
     * 删除商品三级分类信息
     *
     * @param catId 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatId(Long catId) {
        return categoryMapper.deleteCategoryByCatId(catId);
    }

    @Override
    public List<Category> listWithTree() {
        Category category = new Category();
        List<Category> categories = categoryMapper.selectCategoryList(category);
        return categories.stream().filter(categoryFilter -> categoryFilter.getParentCid() == 0).map(categoryMap -> {
            categoryMap.setChildren(getChildrenList(categoryMap, categories));
            return categoryMap;
        }).sorted((MenuFirst, MenuLate) -> {
            return (int) ((MenuFirst.getSort()==null?0:MenuFirst.getSort()) - (MenuLate.getSort()==null?0:MenuLate.getSort()));
        }).collect(Collectors.toList());
    }

    public List<Category> getChildrenList(Category root, List<Category> all) {
        List<Category> collect = all.stream().filter(category -> Objects.equals(category.getParentCid(), root.getCatId())).map(category -> {
            category.setChildren(getChildrenList(category, all));
            return category;
        }).sorted((MenuFirst, MenuLate) -> {
            return (int) ((MenuFirst.getSort()==null?0:MenuFirst.getSort()) - (MenuLate.getSort()==null?0:MenuLate.getSort()));
        }).collect(Collectors.toList());
        return collect;
    }
}
